package com.army.stock.service.impl;

import com.army.stock.dao.ArmyRoleDao;
import com.army.stock.domain.ArmyRole;
import com.army.stock.service.ArmyRoleService;
import com.javalife.common.domain.page.PaginatedArrayList;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 15/3/3.
 */
public class ArmyRoleServiceImpl implements ArmyRoleService {

    private ArmyRoleDao armyRoleDao;

    public void setArmyRoleDao(ArmyRoleDao armyRoleDao) {
        this.armyRoleDao = armyRoleDao;
    }

    @Override
    public PaginatedArrayList getArmyRolePageInfo(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyRoleDao.getCountArmyRole(map);
        List<ArmyRole> armyRoleList = armyRoleDao.getArmyRoleList(map);
        PaginatedArrayList<ArmyRole> paginatedArrayList = new PaginatedArrayList<ArmyRole>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(armyRoleList);
        return paginatedArrayList;
    }

    public void addArmyRole(ArmyRole armyRole) {
        armyRoleDao.insertArmyRole(armyRole);
    }

    public ArmyRole getArmyRoleById(Long id) {
        return armyRoleDao.getArmyRoleById(id);
    }

    public void updateArmyRole(ArmyRole armyRole) {
        armyRoleDao.updateArmyRole(armyRole);
    }

    @Override
    public List<Map> getAllPermitsByRoleId(Long roleId) {
        List<Map> permits = armyRoleDao.getAllPermits();
        List<String> rolePermits = armyRoleDao.getPermitsByRoleId(roleId);
        boolean isEmpty = CollectionUtils.isEmpty(rolePermits);
        //组装json对象
        for (Map permit : permits) {
            String id = (String) permit.get("id");
            String name = (String) permit.get("name");
            String pId = (String) permit.get("pId");
            permit.put("open", true);
            if (!isEmpty && rolePermits.contains(id)) {
                permit.put("checked", true);
            }
        }
        return permits;
    }

    public void updateRolePermits(String roleId, String permits) {
        if (StringUtils.isNotBlank(roleId) && StringUtils.isNotBlank(permits)) {
            //删除原有的权限
            armyRoleDao.deletePermitsByRoleId(Long.valueOf(roleId));
            String[] arr = StringUtils.split(permits, ',');
            Map map = new HashMap();
            for (String temp : arr) {
                map.put("roleId", roleId);
                map.put("permitId", temp);
                armyRoleDao.insertArmyRolePermit(map);
            }
        }
    }

    public String getPermitsByUserId(Long userId) {
        List<String> permitList = armyRoleDao.getAllpermitsByUserId(userId);
        return StringUtils.join(permitList, ',');
    }

    public List getAllArmyRoleByUserId(String userId) {
        List<Map> roleList = armyRoleDao.getAllArmyRoleList();
        List<Long> userRoleIdList = armyRoleDao.getUserRoleListByUserId(Long.valueOf(userId));
        if (CollectionUtils.isNotEmpty(roleList)) {
            for (Map role : roleList) {
                Long roleId = (Long) role.get("id");
                if (userRoleIdList.contains(roleId)) {
                    role.put("hit", "1");
                } else {
                    role.put("hit", "0");
                }
            }
        }
        return roleList;
    }

    public void updateUserRole(String userId, String roleIds) {
        //删除用户原有的角色
        armyRoleDao.deleteUserRole(Long.valueOf(userId));
        //插入到army_user_role 表
        if (StringUtils.isNotBlank(roleIds)) {
            Map map = new HashMap();
            String[] arr = StringUtils.split(roleIds, ',');
            map.put("userId", Long.valueOf(userId));
            for (String roleId : arr) {
                map.put("roleId", roleId);
                armyRoleDao.insertUserRole(map);
            }
        }
    }

}
